package com.albert.core.controller.admin;



import com.albert.common.exception.BusinessException;
import com.albert.common.result.R;
import com.albert.common.result.ResponseEnum;
import com.albert.core.pojo.entity.IntegralGrade;
import com.albert.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.albert.common.exception.Assert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Hikari
 * @since 2024-07-21
 */
@Api(tags="积分等级制度")
@RestController
@Slf4j
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    @ApiOperation("积分等级制度列表查找")
    public R listAll(){
        List<IntegralGrade> list = integralGradeService.list();
        log.info("log info");
        log.trace("log trace");
        log.debug("log debug");
        log.error("log error");
        log.warn("log warn");
        return R.ok().data("list",list).message("获取列表成功");
    }
    @ApiOperation("根据ID删除")
    @DeleteMapping("/remove/{id}")
    public R delete(
            @ApiParam(value="数据ID",example="007",required = true)
            @PathVariable Long id){
        boolean b = integralGradeService.removeById(id);
        if(b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }
    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save( @ApiParam(value="积分对象等级",required = true)
            @RequestBody IntegralGrade grade){
//        if(grade.getBorrowAmount() == null){
//            //BORROW_AMOUNT_NULL_ERROR(-201, "借款额度不能为空"),
//            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
//        }
        Assert.notNull(grade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(grade);
        if(result){
            return R.ok().message("新增成功");
        }else{
            return R.error().message("保存失败");
        }
    }
    @ApiOperation("根据ID获取积分等级")
    @GetMapping("/getById/{id}")
    public R getById(@ApiParam(value="",required = true,example = "1")
                     @PathVariable Long id){
        IntegralGrade result = integralGradeService.getById(id);
        if(result!=null){
            return R.ok().data("record",result);
        }else{
            return R.error().message("数据获取失败");
        }
    }
    @ApiOperation("修改积分等级")
    @PutMapping("/update")
    public R update( @ApiParam(value="积分对象等级",required = true)
                   @RequestBody IntegralGrade grade){
        boolean result = integralGradeService.updateById(grade);
        if(result){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }

}

